package cn.shujuhai.cms.config;


import cn.dev33.satoken.stp.StpUtil;
import cn.shujuhai.cms.domain.InfoArticle;
import cn.shujuhai.cms.domain.LogArticle;
import cn.shujuhai.cms.domain.LogChannel;
import cn.shujuhai.cms.domain.SysChannel;
import cn.shujuhai.cms.service.InfoArticleService;
import cn.shujuhai.cms.service.LogArticleService;
import cn.shujuhai.cms.service.LogChannelService;
import cn.shujuhai.cms.service.SysChannelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @Author: NiuYaHu
 * @Date: 2025/8/14 09:45
 * @Description:
 **/
@Component
@RequiredArgsConstructor
public class StaticPageInterceptor implements HandlerInterceptor {
    private final SysChannelService sysChannelService;
    private final InfoArticleService infoArticleService;
    private final LogArticleService logArticleService;
    private final LogChannelService logChannelService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取请求的 URI 部分
        String path = request.getRequestURI();
        System.out.println(path);
        String substring = path.substring(path.indexOf("/work/") + 6);
        // 分割路径为数组
        String[] parts = substring.split("/");
        // 获取第一个单词
        String firstWord = parts[0];
        // 获取最后一个单词（去掉 ".html" 后的部分）
        String lastWord = parts[parts.length - 1].replace(".html", "");
        // 获取访问时间
        LocalDateTime now = LocalDateTime.now();
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            String accessTime = now.format(formatter);

        // 获取访问者的IP地址
        String ipAddress = request.getRemoteAddr();
        if (Objects.equals(firstWord, "channel")) {
            SysChannel sysChannel = this.sysChannelService.getOne(new QueryWrapper<SysChannel>().lambda().eq(SysChannel::getUrlKey, lastWord));
            sysChannel.setCount(sysChannel.getCount() + 1);
            this.sysChannelService.updateById(sysChannel);
            LogChannel logChannel = new LogChannel();
            logChannel.setChannelId(sysChannel.getChannelId());
            logChannel.setIp(ipAddress);
            logChannel.setSystime(now);
            this.logChannelService.save(logChannel);

        } else {
            InfoArticle infoArticle = this.infoArticleService.getOne(new QueryWrapper<InfoArticle>().lambda().eq(InfoArticle::getArticleId, Long.valueOf(lastWord)));
            infoArticle.setCount(infoArticle.getCount() + 1);
            this.infoArticleService.updateById(infoArticle);
            LogArticle logArticle = new LogArticle();
            logArticle.setArticleId(infoArticle.getArticleId());
            logArticle.setIp(ipAddress);
            logArticle.setSystime(now);
            this.logArticleService.save(logArticle);
        }
        // 继续处理请求
        return true;
    }
}